<!DOCTYPE html>
<html>
  <head>
    <title id="windowtitle"> PhreshPlayer </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="img/phreshplayer-icon.ico" rel="shortcut icon" />
    <link href="css/w3.css" rel="stylesheet" />
    <script src="js/w3.js"></script>

    <link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" />
    <link href="css/chrome.custom.scrollbar.css" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
    <link id="themecss" rel="stylesheet" href="css/themes/phresh.css" />

    <script>const i18n = new(require('../locales/i18n'));</script>
  </head>
  <body>
    <div id="content">
      
      <video id='videoplayer' class="w3-left" ondblclick="toggleFullScreen();" src="" poster="img/phreshplayer-logo_old.png" crossorigin="anonymous" allowfullscreen></video>
      
      <textarea id="subtitleContainer" style="display:none;"></textarea>
      
      <div id="toastsholder">
        <div id="toasts" class="w3-animate-zoom color" style="display:none;"></div>
      </div>

      <div id="controlsholder">
        <div id="controls">

          <div style="margin-bottom:15px;">

            <span id="playing" class="clickable" data="0"> PhreshPlayer </span>

            <a id="fullscreen" onclick="toggleFullScreen();" class="w3-right clickable">
              <i class="fa fa-arrows-alt w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('fullscreen'));</script> </span>
              </i>
            </a>

          </div>

          <div id="playprogressholder" class="progressholder w3-round-xlarge">
            <div id="playprogress" class="progress w3-round-xlarge w3-center" style="width:0%;"></div>
          </div>

          <div id="elapsedtime" class="w3-left w3-tiny colorOnHover clickable"> 00:00:00 </div>
          <div id="reamingtime" class="w3-right w3-tiny colorOnHover clickable"> -00:00:00 </div>
          <div class="w3-clear" style="margin-bottom:10px;"></div>

          <div id="controls-main-row" style="margin-bottom:10px;">

            <a class="clickable" onclick="playerjs.playPrev();">
              <i class="fa fa-fast-backward w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('previous'));</script> </span>
              </i>
            </a>

            <a class="clickable" onclick="playerjs.seekVideo('backward');">
              <i class="fa fa-backward w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('backward'));</script> </span>
              </i>
            </a>

            <a id="playbutton" class="clickable" onclick="playerjs.handlePlayPause('play');">
              <i class="fa fa-play w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('play'));</script> </span>
              </i>
            </a>
            <a id="pausebutton" class="clickable" onclick="playerjs.handlePlayPause('pause');" style="display:none;">
              <i class="fa fa-pause w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('pause'));</script> </span>
              </i>
            </a>

            <a id="forward" class="clickable" onclick="playerjs.seekVideo('forward');">
              <i class="fa fa-forward w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('forward'));</script> </span>
              </i>
            </a>
            
            <a id="next" class="clickable" onclick="playerjs.playNext();">
              <i class="fa fa-fast-forward w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('next'));</script> </span>
              </i>
            </a>


            <a id="loopbutton" class="clickable" onclick="playerjs.loopVideo();" style="margin-left:20px;">
              <i class="fa fa-random w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('loop') + ' ' + i18n.__('on') + '/' + i18n.__('off'));</script> </span> </span>
              </i>
            </a>

            
            <a id="mutebutton" class="clickable" onclick="playerjs.volumeMute();" style="margin-left:20px;">
              <i class="fa fa-bell-slash-o w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('mute') + ' ' + i18n.__('on') + '/' + i18n.__('off'));</script> </span>
              </i>
            </a>

            
            <a id="controlsalwaystotop" class="clickable" onclick="toggleControlsAwaysOnTop();" style="margin-left:10px;">
              <i class="fa fa-thumb-tack w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('controlsalwaysontop') + ' ' + i18n.__('on') + '/' + i18n.__('off'));</script> </span>
              </i>
            </a>

            
            <a id="plugins" class="clickable" onclick="showHidePlugins();" style="margin-left:10px;">
              <i class="fa fa-plug w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('plugins'));</script> </span>
              </i>
            </a>
            
          </div>

          <div id="controls-sub-row" style="margin-bottom:10px;">

            <a id="playlistbutton" class="w3-right clickable activated" onclick="playlistjs.toggleShowPlaylist();" style="margin-left:20px;">
              <i class="fa fa-list w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('playlist') + ' ' + i18n.__('on') + '/' + i18n.__('off'));</script> </span>
              </i>
            </a>


            <a class="w3-right clickable" onclick="playerjs.changeSubtitle();">
              <i class="fa fa-commenting-o w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('subtitle'));</script> </span>
              </i>
              <span id="currSubNum">0</span>/<span id="allSubNum">0</span>
            </a>

            <a class="w3-right clickable" onclick="playerjs.changeAudioTrack();">
              <i class="fa fa-comment-o w3-tooltip" aria-hidden="true">
                <span class="w3-text w3-tag w3-tooltip-absolute w3-round-large w3-animate-zoom"> <script>document.write(i18n.__('sound_track'));</script> </span>
              </i>
              <span id="currLangNum">0</span>/<span id="allLangNum">0</span>
            </a>

          </div>

        </div>
      </div>

      <input id="searchinplaylist" type="text" onkeyup="playlistjs.searchInPlaylist(this.value);" />
      <i id="searchinplaylisticon" class="fa fa-search color" onclick="document.getElementById('searchinplaylist').focus();"></i>

      <ol id="playlist" class="w3-left"></ol>
      
    </div>

    <div id="loading-overlay" class="closedoverlay">
      <i class="fa fa-5x fa-pulse fa-spinner" aria-hidden="true"></i>
    </div>

    <!-- Modals -->

    <div id="pluginsmodal" class="w3-modal">
      <div class="w3-modal-content w3-animate-zoom w3-card-4">
        <header class="w3-container">
          <span onclick="showHidePlugins();" class="w3-button w3-right w3-round w3-hover-white">&times;</span>
          <span onclick="document.location.href='index.html';" class="w3-button w3-right w3-round w3-hover-white"><i class="fa fa-refresh"></i></span>
          <h2> <script>document.write(i18n.__('plugins'));</script> </h2>
        </header>
        <div class="w3-modal-content">
          <div id="pluginsmodalcontent" class="w3-container">
            <!-- fill it with plugins -->
          </div>
        </div>
      </div>
    </div>

    <div id="modalsholder"></div>

    <script>
      const windowtitle = document.getElementById('windowtitle');
      const content = document.getElementById('content');
      const toasts = document.getElementById('toasts');
      const videoplayer = document.getElementById('videoplayer');
      const subtitleContainer = document.getElementById("subtitleContainer");
      const playing = document.getElementById('playing');
      const controls = document.getElementById('controls');
      const controlsMainRow = document.getElementById('controls-main-row');
      const controlsSubRow = document.getElementById('controls-sub-row');
      const playbutton = document.getElementById('playbutton');
      const pausebutton = document.getElementById('pausebutton');
      const currSubNum = document.getElementById('currSubNum');
      const allSubNum = document.getElementById('allSubNum');
      const currLangNum = document.getElementById('currLangNum');
      const allLangNum = document.getElementById('allLangNum');
      const playlistholder = document.getElementById('playlistholder');
      const playlist = document.getElementById('playlist');
      const searchinplaylist = document.getElementById('searchinplaylist');
      const searchinplaylisticon = document.getElementById('searchinplaylisticon');
      const playprogressholder = document.getElementById('playprogressholder');
      const playprogress = document.getElementById('playprogress');
      const elapsedtime = document.getElementById('elapsedtime');
      const reamingtime = document.getElementById('reamingtime');
      const loadingoverlay = document.getElementById('loading-overlay');
      const modalsholder = document.getElementById('modalsholder');
      const pluginsmodal = document.getElementById('pluginsmodal');
      const pluginsmodalcontent = document.getElementById('pluginsmodalcontent');

      let d = new Date();
      let nowYear = d.getFullYear();
      let nowMonth = d.getMonth() + 1;
      let nowDay = d.getDate();
      const todayDate = nowYear + "-" + nowMonth + "-" + nowDay;

      const os = process.platform;
      const desktopEnv = require('desktop-env');
      const electron = require('electron');
      const {remote, ipcRenderer} = require('electron');
      const app = electron.app || electron.remote.app;
      const currentWindow = remote.getCurrentWindow();
      const Mousetrap = require('mousetrap');
      const {dialog} = require('electron').remote;
      const fse = require('fs-extra');
      const http = require('http');
      const path = require('path');
      const srt2vtt = require('srt2vtt');
      const request = require('request');
      const unzipper = require('unzipper');
      const co = require('co');
      const npminstall = require('npminstall');
      
      const Datastore = require('nedb');
      const appDir = app.getPath('userData');
      const pluginsDir = path.join(appDir, 'plugins');
      const pluginsfile = new Datastore({ filename: appDir + '/plugins.db', autoload: true });
      const playlistfile = new Datastore({ filename: appDir + '/playlist.db', autoload: true });

      const Store = require('electron-store');
      const store = new Store();
      
      if (!store.has('settings.pulginslastsync')) {
        store.set('settings.pulginslastsync', '2019-7-1');
      }
      
      if (store.has('settings.theme')) {
        changeTheme(store.get('settings.theme'));
      } else {
        store.set('settings.theme', 'phresh');
        changeTheme('phresh');
      }

      if (store.has('settings.volume')) {
        let currVolume = store.get('settings.volume');
        videoplayer.volume = currVolume;
      }

      if (store.has('settings.alwaysontop')) {
        let alwaysOnTop = store.get('settings.alwaysontop');
        if (alwaysOnTop) {
          currentWindow.setAlwaysOnTop(true);
        } else {
          currentWindow.setAlwaysOnTop(false);
        }
      } else {
        store.set('settings.alwaysontop', false);
      }

      if (store.has('settings.showplaylist')) {
        let showPlaylist = store.get('settings.showplaylist');
        if (showPlaylist) {
          playlist.style.display = 'block';
          videoplayer.style.width = '60%';
          w3.addClass('#playlistbutton','activated');
        } else {
          playlist.style.display = 'none';
          videoplayer.style.width = '100%';
          w3.removeClass('#playlistbutton','activated');
        }
      } else {
        store.set('settings.showplaylist', true);
      }

      let showSubtitle;

      if (store.has('settings.showsubtitle')) {
        showSubtitle = store.get('settings.showsubtitle');
      } else {
        store.set('settings.showsubtitle', false);
        showSubtitle = false;
      }
      
      if (!store.has('settings.subtitlelanguage')) {

        if (store.has('settings.language')) {
          let storedLanguage = store.get('settings.language');
          store.set('settings.subtitlelanguage', storedLanguage);
        } else {
          store.set('settings.subtitlelanguage', 'en');
        }
        
      }

      /* audio lang come here, at one time.. */
      /*
      as i see never, cuz' up to chromium v4
      stopped the autostart videos and i cant get around..
      plus the audio tracks not supported even now. Electron v5.0.6 (6.0.0-beta) - 2019-07-10
      */
    
      let supportedFileTypes = [
        "video/webm",
        "video/x-matroska",
        "video/mp4",
        "video/ogg",
        "video/quicktime",
        "audio/mp3",
        "audio/flac"
      ];
      let supportedFileExts = [
        ".webm",
        ".mkv",
        ".mp4",
        ".ogg",
        ".mov",
        ".mp3",
        ".flac"
      ];

      // let w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
      let h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
      content.style.height = h + "px";
      videoplayer.style.height = h + "px";
      playlist.style.height = h + "px";

      /* Listeners */

      electron.ipcRenderer.on('callFunction', function(event, functionName, functionParam) {
        switch (functionName) {
          case"setTheme":
            changeTheme(functionParam);
            break;
          case"setLanguage":
            setLanguage(functionParam);
            break;
          case"setSubtitleLanguage":
            setSubtitleLanguage(functionParam);
            break;
          case"toggleFullScreen":
            toggleFullScreen();
            break;
          case"toggleAllwaysOnTop":
            toggleAllwaysOnTop();
            break;
          /* Player */
          case"volumeMute":
            playerjs.volumeMute();
            break;
          case"loopVideo":
            playerjs.loopVideo();
            break;
          case"toggleShowSubtitle":
            playerjs.toggleShowSubtitle();
            break;
          /* Playlist */
          case"toggleShowPlaylist":
            playlistjs.toggleShowPlaylist();
            break;
          case"savePlaylist":
            playlistjs.savePlaylist();
            break;
        }
      });

      currentWindow.on('resize', function () {
        // w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
        h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
        content.style.height = h + "px";
        videoplayer.style.height = h + "px";
        playlist.style.height = h + "px";
      });

      let controlsIsVisible;
      let hideControlsAlowedGlobally = true;
      let hideControlsAlowed = true;

      if (store.has('settings.controlsalwaysontop')) {
        let isControlsAlwaysOnTop = store.get('settings.controlsalwaysontop');
        if (isControlsAlwaysOnTop) {
          hideControlsAlowedGlobally = false;
          w3.addClass('#controlsalwaystotop','activated');
        } else {
          hideControlsAlowedGlobally = true;
          w3.removeClass('#controlsalwaystotop','activated');
        }
      } else {
        store.set('settings.controlsalwaysontop', false);
      }

      document.addEventListener("mousemove",function(e) {
        showControls();
        hideControls();
      }, false);

      controls.addEventListener("mouseenter",function(e) {
        hideControlsAlowed = false;
        document.removeEventListener("mousemove", showControls(), false);
      }, false);
      controls.addEventListener("mouseleave",function(e) {
        if (hideControlsAlowedGlobally) {
          hideControlsAlowed = true;
          document.addEventListener("mousemove", hideControls(), false);
        }
      }, false);

      videoplayer.addEventListener("timeupdate",function(e) {
        
        let mediaDuration = videoplayer.duration;
        let currTime = videoplayer.currentTime;
        let reamingTime = mediaDuration - currTime;
        let currPercentage = Math.floor(100 * currTime / mediaDuration);
        let currPercentageToTasKbar = Math.max(currPercentage / 100);

        if (reamingTime <= 1) {
          playerjs.playNext();
        }

        playprogress.style.width = currPercentage + '%';
        playprogress.innerHTML = currPercentage + '%';

        elapsedtime.innerHTML = getVideoTime(currTime*1000);
        reamingtime.innerHTML = '- ' + getVideoTime(reamingTime*1000);

        if (os === 'linux') {
          desktopEnv().then(currEnv => {
            if(currEnv === 'Unity') { 
              if (!isNaN(currPercentageToTasKbar)) {
                currentWindow.setProgressBar(currPercentageToTasKbar);
              }
            }
          });
        } else {
          if (!isNaN(currPercentageToTasKbar)) {
            currentWindow.setProgressBar(currPercentageToTasKbar);
          }
        }
        
      }, false);

      videoplayer.addEventListener("mousewheel",function(e) {
        let delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
        if (delta === 1) {
          playerjs.volumeControl("up");
        } else if (delta === -1) {
          playerjs.volumeControl("down");
        }
      }, false);

      videoplayer.addEventListener("error",function(e) {
        console.log("error listener:");
        console.log(e);
        let playedId = playing.getAttribute('data');
        document.getElementById('vid-' + playedId).style.color = 'red';
        playerjs.playNext();
      }, false);

      playprogressholder.addEventListener("click",function(e) {
        let percent = e.offsetX / this.offsetWidth;
        videoplayer.currentTime = percent * videoplayer.duration;
        playprogress.style.width = percent / 100 + "%";
      }, false);

      // the only one working solution, for all possible close events
      window.onbeforeunload = (e) => {
        console.log('Save played before quit..');
        store.set('lastplayed.videoid', playing.getAttribute("data"));
        store.set('lastplayed.videotime', videoplayer.currentTime);
      }

      /* File drops */

      playlist.ondragover = () => { return false; };
      playlist.ondragleave = () => { return false; };
      playlist.ondragend = () => { return false; };
      playlist.ondrop = (e) => {
        e.preventDefault();

        let newid = playlist.getElementsByTagName('li').length;
        let newvideos = [];
        
        let iDroppedItemIdx = 0;

        for (let f of e.dataTransfer.files) {

          let filePath = f.path;
          let fileName = f.name;

          if (fileName === '') {
            /* On Linux f.name is empty when contains unicode characters (é, á, etc) */
            let filePathParts = filePath.split('/');
            let lastFilePathPart = filePathParts.length - 1;
            fileName = filePathParts[lastFilePathPart];
          }

          let droppedIsDir = fse.lstatSync(filePath).isDirectory();
          
          if (droppedIsDir) {

            let folderFiles = fse.readdirSync(filePath);
            for (let ii = 0; ii < folderFiles.length; ii++) {

              let fileInFolderPath = path.join(filePath, folderFiles[ii]);
              let fileInFolderName = folderFiles[ii];
              let fileInFolderExt = path.extname(folderFiles[ii]);

              if ( supportedFileExts.indexOf(fileInFolderExt) !== -1 ) {

                newid++;
                let newvideo = {
                  _id: newid,
                  _name: fileInFolderName,
                  _path: fileInFolderPath
                };
                newvideos.push(newvideo);
  
              } else {

                if (fileInFolderExt === '.pppl') {

                  /* my own playlist file :P */
                  setTimeout(function() {
                    // w8 a bit while the direct files drop handling
                    playlistjs.readPlaylistFile(fileInFolderPath);
                  }, 300);

                }

              }

            }

          } else {
            
            if ( supportedFileTypes.indexOf(f.type) !== -1 ) {

              newid++;
              let newvideo = {
                _id: newid,
                _name: fileName,
                _path: filePath
              };
              newvideos.push(newvideo);

            } else {

              let fileExt = path.extname(e.dataTransfer.files[iDroppedItemIdx].path);
              
              if (fileExt === '.pppl') {

                /* my own playlist file :P */
                setTimeout(function() {
                  // w8 a bit while the direct files drop handling
                  playlistjs.readPlaylistFile(filePath);
                }, 300);

              }

            }
            
          }

          iDroppedItemIdx++;

        }

        playlistjs.addToPlaylist(newvideos);

        return false;
      };

      videoplayer.ondragover = () => { return false; };
      videoplayer.ondragleave = () => { return false; };
      videoplayer.ondragend = () => { return false; };
      videoplayer.ondrop = (e) => {
        e.preventDefault();

        playlist.innerHTML = '';
        playlistfile.remove({}, { multi: true }, function (err, numRemoved) {});
        
        let newid = 0;
        let newvideos = [];
        
        let iDroppedItemIdx = 0;

        for (let f of e.dataTransfer.files) {

          let filePath = f.path;
          let fileName = f.name;

          if (fileName === '') {
            let filePathParts = filePath.split('/');
            let lastFilePathPart = filePathParts.length - 1;
            fileName = filePathParts[lastFilePathPart];
          }

          let droppedIsDir = fse.lstatSync(filePath).isDirectory();
          
          if (droppedIsDir) {
            
            let folderFiles = fse.readdirSync(filePath);
            for (let ii = 0; ii < folderFiles.length; ii++) {

              let fileInFolderPath = path.join(filePath, folderFiles[ii]);
              let fileInFolderName = folderFiles[ii];
              let fileInFolderExt = path.extname(folderFiles[ii]);

              if ( supportedFileExts.indexOf(fileInFolderExt) !== -1 ) {

                newid++;
                let newvideo = {
                  _id: newid,
                  _name: fileInFolderName,
                  _path: fileInFolderPath
                };
                newvideos.push(newvideo);
  
              } else {

                if (fileInFolderExt === '.pppl') {

                  /* my own playlist file :P */
                  setTimeout(function() {
                    // w8 a bit while the direct files drop handling
                    playlistjs.readPlaylistFile(fileInFolderPath);
                  }, 300);

                }

              }

            }

          } else {
            
            if ( supportedFileTypes.indexOf(f.type) !== -1 ) {

              newid++;
              let newvideo = {
                _id: newid,
                _name: fileName,
                _path: filePath
              };
              newvideos.push(newvideo);

            } else {

              let fileExt = path.extname(e.dataTransfer.files[iDroppedItemIdx].path);
              
              if (fileExt === '.pppl') {

                /* my own playlist file :P */
                setTimeout(function() {
                  // w8 a bit while the direct files drop handling
                  playlistjs.readPlaylistFile(filePath);
                }, 300);

              }

            }
            
          }

          iDroppedItemIdx++;

        }

        playlistjs.addToPlaylist(newvideos);

        setTimeout(function(){ playerjs.playVideo(1); }, 1000);

        return false;
      };

      /* hotkeys */

      Mousetrap.bind('l', function() { playlistjs.toggleShowPlaylist(); });
      Mousetrap.bind(['f', 'enter'], function() {
        toggleFullScreen();
      });
      
      Mousetrap.bind('t', function() { toggleAllwaysOnTop(); });

      Mousetrap.bind('up', function() { playerjs.volumeControl("up"); });
      Mousetrap.bind('down', function() { playerjs.volumeControl("down"); });
      Mousetrap.bind('m', function() { playerjs.volumeMute("m"); });

      Mousetrap.bind('left', function() { playerjs.seekVideo("backward"); });
      Mousetrap.bind('right', function() { playerjs.seekVideo("forward"); });
   
      Mousetrap.bind('n', function() { playerjs.playNext(); });
      Mousetrap.bind('b', function() { playerjs.playPrev(); });
      Mousetrap.bind('p', function() { playerjs.playPrev(); });

      Mousetrap.bind('space', function() {
        if (playbutton.style.display === 'none') {
          playerjs.handlePlayPause('pause');
        } else {
          playerjs.handlePlayPause('play');
        }
      });

      Mousetrap.bind('q', function() {
        currentWindow.close();
      });

      /* functions */
      
      function openExternalLink(link) {
        require('electron').shell.openExternal(link);
      }

      function changeTheme(currAppTheme) {

        store.set('settings.theme', currAppTheme);
        document.getElementById('themecss').href= 'css/themes/' + currAppTheme + '.css';

        switch(currAppTheme) {
          case 'phresh':
            videoplayer.setAttribute('poster', 'img/phreshplayer-logo_old.png');
            break;
          case 'cobalt':
            videoplayer.setAttribute('poster', 'img/phreshplayer-logo_horizontal-striped-green.png');
            break;
          case 'fullmetal':
            videoplayer.setAttribute('poster', 'img/phreshplayer-logo_horizontal-striped-black.png');
            break;
          case 'hellokitty':
            videoplayer.setAttribute('poster', 'img/phreshplayer-logo_horizontal-striped-white.png');
            break;

          default:
            videoplayer.setAttribute('poster', 'img/phreshplayer-logo_old.png');
            break;
        }

      }

      function setLanguage(language) {
        store.set('settings.language', language);
        app.relaunch();
        app.exit(0);
      }

      function setSubtitleLanguage(language) {
        store.set('settings.subtitlelanguage', language);
      }

      function toggleFullScreen() {
        let isInFullScreen = currentWindow.isFullScreen();
        if (isInFullScreen) {
          document.webkitCancelFullScreen();
          currentWindow.setFullScreen(false);
          w3.removeClass('#fullscreen','activated');
          setToast(i18n.__('fullscreen') + ' ' + i18n.__('off'));
        } else {
          currentWindow.setFullScreen(true);
          videoplayer.webkitEnterFullScreen();
          w3.addClass('#fullscreen','activated');
          setToast(i18n.__('fullscreen') + ' ' + i18n.__('on'));
        }
      }

      function toggleAllwaysOnTop() {
        let isAlwaysOnTop = currentWindow.isAlwaysOnTop();
        if (isAlwaysOnTop) {
          store.set('settings.alwaysontop', false);
          currentWindow.setAlwaysOnTop(false);
          setToast(i18n.__('alwaysontop') + ' ' + i18n.__('off'));
        } else {
          store.set('settings.alwaysontop', true);
          currentWindow.setAlwaysOnTop(true);
          setToast(i18n.__('alwaysontop') + ' ' + i18n.__('on'));
        }
      }

      function toggleControlsAwaysOnTop() {
        let isControlsAlwaysOnTop = store.get('settings.controlsalwaysontop');
        if (isControlsAlwaysOnTop) {
          store.set('settings.controlsalwaysontop', false);
          hideControlsAlowedGlobally = true;
          w3.removeClass('#controlsalwaystotop','activated');
          setToast(i18n.__('controlsalwaysontop') + ' ' + i18n.__('off'));
        } else {
          store.set('settings.controlsalwaysontop', true);
          hideControlsAlowedGlobally = false;
          w3.addClass('#controlsalwaystotop','activated');
          setToast(i18n.__('controlsalwaysontop') + ' ' + i18n.__('on'));
        }
      }
      
      let toastInFire;
      function setToast(toast) {
        toasts.innerHTML = toast;
        w3.show("#toasts");
        if (toastInFire === undefined) {
          toastInFire = setTimeout(function(){ w3.hide("#toasts"); }, 3000);
        } else {
          clearTimeout(toastInFire);
          toastInFire = setTimeout(function(){ w3.hide("#toasts"); }, 3000);
        }
      }

      function showControls() {
        clearTimeout(controlsIsVisible);
        controls.style.display = 'block';
        videoplayer.style.cursor = 'default';
        playlist.style.cursor = 'default';
      }
      function hideControls() {
        if (hideControlsAlowed) {
          controlsIsVisible = setTimeout(function(){
            controls.style.display = 'none';
            videoplayer.style.cursor = 'none';
            playlist.style.cursor = 'none';
          }, 1000);
        }
      }

      function showHidePlugins() {

        let pluginsLastSyncDate = store.get('settings.pulginslastsync');

        if (pluginsLastSyncDate != todayDate) {

          pluginsjs.syncPluginsInfo()
          .then( () => {

            store.set('settings.pulginslastsync', todayDate);

            if (pluginsmodal.style.display === 'block') {
              w3.hide('#pluginsmodal');
              w3.removeClass('#plugins','activated');
            } else {
              w3.show('#pluginsmodal');
              w3.addClass('#plugins','activated');
              pluginsjs.getPluginsInfo();
            }

          });

        } else {

          if (pluginsmodal.style.display === 'block') {
            w3.hide('#pluginsmodal');
            w3.removeClass('#plugins','activated');
          } else {
            w3.show('#pluginsmodal');
            w3.addClass('#plugins','activated');
            pluginsjs.getPluginsInfo();
          }

        }

      }

      function getVideoTime(ms) {
        let date = new Date(ms);
        let time = [];

        let tH = date.getUTCHours();
        let tM = date.getUTCMinutes();
        let tS = date.getUTCSeconds();
        
        if ( tH.toString().length === 1 ) {
          tH = '0' + tH;
        }
        if ( tM.toString().length === 1 ) {
          tM = '0' + tM;
        }
        if ( tS.toString().length === 1 ) {
          tS = '0' + tS;
        }

        time.push(tH);
        time.push(tM);
        time.push(tS);

        return time.join(':');
      }
      
      const playerjs = require('./js/player.js');
      const playlistjs = require('./js/playlist.js');
      const pluginsjs = require('./js/plugins.js');
      
    </script>
  </body>
</html>
